package com.example.planetexploder.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.constants.LogConst;
import com.example.planetexploder.constants.SortConst;

import java.util.ArrayList;

/**
 * 摘抄表操作集
 * @author 宋熙然
 * 修改日期：2019.10.27
 * 完成测试，修复若干bug
 */
public class BookTable {

    private final DBHelper helper;

    public BookTable(Context context) {
        helper = new DBHelper(context);
        helper.getWritableDatabase();
    }

    /**
     * 创建摘抄集表
     * @param bookId 摘抄集id
     */
    public void create_BookTable(String bookId) {
        Log.d(LogConst.MTAG, "create_BookTable()");
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "CREATE TABLE " + bookId + BookInfo.BOOKTABLE_HEADER;
        db.execSQL(sql);
        db.close();
    }

    /**
     * 删除摘抄集表
     * @param bookId 摘抄集id
     */
    public void delete_BookTable(String bookId) {
        Log.d(LogConst.MTAG, "delete_BookTable()");
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "DROP TABLE " + bookId + ";";
        db.execSQL(sql);
        db.close();
    }

    /**
     * 向摘抄集表中添加摘抄项信息
     * @param itemInfo 摘抄项信息
     */
    public void add_ItemInfo_To_BookTable(String bookId, ItemInfo itemInfo) {
        Log.d(LogConst.MTAG, "add_ItemInfo_To_BookTable()");
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(BookInfo.BOOKTABLE_ITEM_ID, itemInfo.itemId);
        cv.put(BookInfo.BOOKTABLE_ITEM_NAME, itemInfo.itemName);
        cv.put(BookInfo.BOOKTABLE_DATE_MOD, itemInfo.dateMod);
        db.insert(bookId, null, cv);
        db.close();
    }

    /**
     * 从摘抄集表中删除摘抄项信息
     * @param itemId 摘抄项id
     */
    public void delete_ItemInfo_From_BookTable(String bookId, String itemId) {
        Log.d(LogConst.MTAG, "delete_ItemInfo_From_BookTable()");
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "DELETE FROM " + bookId + " WHERE " + BookInfo.BOOKTABLE_ITEM_ID + " = ?;";
        db.execSQL(sql, new Object[]{itemId});
        db.close();
    }

    /**
     * 更新摘抄项信息
     * @param bookId   摘抄集id
     * @param itemInfo 摘抄项信息
     */
    public void update_ItemInfo(String bookId, ItemInfo itemInfo) {
        Log.d(LogConst.MTAG, "update_ItemInfo()");
        SQLiteDatabase db = helper.getWritableDatabase();
        String sql = "UPDATE " + bookId + " SET "
                + BookInfo.BOOKTABLE_ITEM_NAME + "=?,"
                + BookInfo.BOOKTABLE_DATE_MOD + "=? "
                + "WHERE " + BookInfo.BOOKTABLE_ITEM_ID + "=?;";
        db.execSQL(sql, new Object[]{itemInfo.itemName, itemInfo.dateMod, itemInfo.itemId});
        db.close();
    }

    /**
     * 获得摘抄集表中的信息
     * @param bookId 摘抄集id
     * @param order  排序方式
     * @see SortConst 排序方式相关常量
     * @return       摘抄集表中的信息
     */
    public ArrayList<ItemInfo> get_BookTable(String bookId, int order) {
        Log.d(LogConst.MTAG, "get_BookTable()");
        ArrayList<ItemInfo> list = new ArrayList<>();
        SQLiteDatabase db = helper.getReadableDatabase();

        String orderBy;
        if(order == SortConst.ORDER_BY_CRE_UP) { //按创建日期升序
            orderBy = BookInfo.BOOKTABLE_ITEM_ID + " ASC";
        }
        else if(order == SortConst.ORDER_BY_CRE_DOWN) {  //按创建日期降序
            orderBy = BookInfo.BOOKTABLE_ITEM_ID + " DESC";
        }
        else /*if(order == SortConst.ORDER_BY_MOD)*/ {  //按修改日期（降序）
            orderBy = BookInfo.BOOKTABLE_DATE_MOD + " DESC";
        }

        Cursor c = db.query(bookId, null, null, null, null, null, orderBy);
        while (c.moveToNext()){
            ItemInfo itemInfo = new ItemInfo();
            itemInfo.itemId = c.getString(c.getColumnIndex(BookInfo.BOOKTABLE_ITEM_ID));
            itemInfo.itemName = c.getString(c.getColumnIndex(BookInfo.BOOKTABLE_ITEM_NAME));
            itemInfo.dateMod = c.getString(c.getColumnIndex(BookInfo.BOOKTABLE_DATE_MOD));
            list.add(itemInfo);
        }

        c.close();
        db.close();
        return list;
    }

}
